var registerFormOnsubmitHandler = function() {
	var secondPasswordMsg = document.getElementById('second-pswd-err-msg');
	var spans = secondPasswordMsg.children;
	if (spans.length >= 1 && spans[0].style.display != 'none') {
		return false;
	}
};

var firstPasswordOnblurHandler = function() {
	var firstPasswordMsg = document.getElementById('first-pswd-err-msg');
	var firstPassword = document.getElementById('first-pswd');
	var spans = firstPasswordMsg.children;
	if (spans.length >= 3) {
		for (var i = 0; i < spans.length; ++i) {
			spans[i].style.display= 'none';
		}
		var points = check_password_safety(firstPassword.value);
		if( points >= 24 ) {
			spans[2].style.display= '';
		} else if( points >= 16 ) {
			spans[1].style.display = '';
		} else {
			spans[0].style.display = '';
		}
	}
};

var secondPasswordOnblurHandler = function() {
	var secondPasswordMsg = document.getElementById('second-pswd-err-msg');
	var firstPassword = document.getElementById('first-pswd');
	var secondPassword = document.getElementById('second-pswd');
	var spans = secondPasswordMsg.children;
	if (spans.length >= 1) {
		spans[0].style.display= 'none';
		if(firstPassword.value != secondPassword.value) {
			spans[0].style.display= '';
		}
	}
};

var check_password_safety = function(pwd){

	var msg = "";
	var points = pwd.length;
	var password_info = document.getElementById('password_info');

	var has_letter		= new RegExp("[a-z]");
	var has_caps		= new RegExp("[A-Z]");
	var has_numbers		= new RegExp("[0-9]");
	var has_symbols		= new RegExp("\\W");

	if(has_letter.test(pwd)) 	{ points += 4; }
	if(has_caps.test(pwd)) 		{ points += 4; }
	if(has_numbers.test(pwd)) 	{ points += 4; }
	if(has_symbols.test(pwd)) 	{ points += 4; }

	return points;
};

window.onload = function() {
	var registerForm = document.getElementById('registration-form');
	registerForm.onsubmit = registerFormOnsubmitHandler;
	var firstPassword = document.getElementById('first-pswd');
	firstPassword.onblur = firstPasswordOnblurHandler;
	var secondPassword = document.getElementById('second-pswd');
	secondPassword.onblur = secondPasswordOnblurHandler;
};